const { chromium } = require("playwright");

(async () => {
  const browser = await chromium.launch();
  const context = await browser.newContext();
  const page = await context.newPage();

  try {
    // 打开百度
    await page.goto("https://www.baidu.com");

    // 输入搜索关键词并回车
    await page.fill("textarea#chat-textarea", "Playwright 自动化");
    await page.keyboard.press("Enter");

    // 等待搜索结果加载
    await page.waitForSelector("#content_left");

    console.log("百度搜索执行成功");
  } catch (e) {
    console.error("自动化执行失败:", e);
    process.exitCode = 1;
  } finally {
    // 保持几秒以方便观察，再关闭
    await page.waitForTimeout(2000);
    await browser.close();
  }
})();
